Extract parts from alfanumerical values
The substr()
function can be used to extract parts from alfanumerical values. The first input parameter declares name of variable, the next one the start position of the reading, while the last parameter assigns the number of positions to be read from the starting position. For example: substr(municipality, 1, 2)
will read and assign the first two letters from all values for the variable municipality.
//Connect to datastore
require no.ssb.fdb:23 as db
create-dataset demography
import db/BEFOLKNING_KOMMNR_FAKTISK 2021-01-01 as municipality
//Recode from municipality to county level
generate county = substr(municipality, 1, 2)
//Adding value labels for nicer output
define-labels countystring '03' Oslo '11' Rogaland '15' 'Møre og Romsdal' '18' Nordland '30' Viken '34' Innlandet '38' 'Vestfold og Telemark' '42' Agder '46' Vestland '50' Trøndelag '54' 'Troms og Finnmark' '21' Spitsbergen '25' 'Education abroad' '99' Unknown
assign-labels county countystring
tabulate county